<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
 <head>
  <meta http-equiv="content-type" content="text/html; charset=UTF-8">
  <title>注册一个用 PHP 类实现的 URL 封装协议</title>
 </head>
 <body class="docs"><div id="layout">
  <div id="layout-content"><div id="function.stream-wrapper-register" class="refentry">
   <div class="refnamediv">
    <h1 class="refname">stream_wrapper_register</h1>
    <p class="verinfo">(PHP 4 &gt;= 4.3.2, PHP 5, PHP 7)</p><p class="refpurpose"><span class="refname">stream_wrapper_register</span> &mdash; <span class="dc-title">注册一个用 PHP 类实现的 URL 封装协议</span></p>

   </div>
   <div class="refsect1 description" id="refsect1-function.stream-wrapper-register-description">
  <h3 class="title">说明</h3>
  <div class="methodsynopsis dc-description">
     <span class="methodname"><strong>stream_wrapper_register</strong></span>
      ( <span class="methodparam"><span class="type">string</span> <code class="parameter">$protocol</code></span>
     , <span class="methodparam"><span class="type">string</span> <code class="parameter">$classname</code></span>
     [, <span class="methodparam"><span class="type">int</span> <code class="parameter">$flags</code> <span class="initializer"> = 0</span></span>
    ] ) : <span class="type">bool</span></div>

    <p class="para rdfs-comment">
     允许用户实现自定义的协议处理器和流，用于所有其它的文件系统函数中（例如
     <span class="function"><a href="fopen.html" class="function">fopen()</a></span>，<span class="function"><a href="fread.html" class="function">fread()</a></span> 等）。
    </p>
    </div>

   
   <div class="refsect1 parameters" id="refsect1-function.stream-wrapper-register-parameters">
  <h3 class="title">参数</h3>
    <p class="para">
   <dl>

    
     <dt>
<code class="parameter">protocol</code></dt>

     <dd>

      <p class="para">
       待注册的封装的名字。
      </p>
     </dd>

    
    
     <dt>
<code class="parameter">classname</code></dt>

     <dd>

      <p class="para">
       实现了<code class="parameter">protocol</code>的类名。
      </p>
     </dd>

    
    
     <dt>
<code class="parameter">flags</code></dt>

     <dd>

      <p class="para">
       Should be set to <strong><code>STREAM_IS_URL</code></strong> if
       <code class="parameter">protocol</code> is a URL protocol. Default is 0, local
       stream.
      </p>
     </dd>

    
   </dl>

  </p>
 </div>

   
   
 <div class="refsect1 returnvalues" id="refsect1-function.stream-wrapper-register-returnvalues">
  <h3 class="title">返回值</h3>
  <p class="para">
   成功时返回 <strong><code>TRUE</code></strong>， 或者在失败时返回 <strong><code>FALSE</code></strong>。
  </p>
  <p class="para">
    当 <code class="parameter">protocol</code> 已经有处理者时，<span class="function"><strong>stream_wrapper_register()</strong></span> 将返回<strong><code>FALSE</code></strong> 
  </p>
 </div>


 <div class="refsect1 changelog" id="refsect1-function.stream-wrapper-register-changelog">
  <h3 class="title">更新日志</h3>
  <p class="para">
   <table class="doctable informaltable">
    
     <thead>
      <tr>
       <th>版本</th>
       <th>说明</th>
      </tr>

     </thead>

     <tbody class="tbody">
      <tr>
       <td>5.2.4</td>
       <td>
        添加 <code class="parameter">flags</code> 参数.
       </td>
      </tr>

     </tbody>
    
   </table>

  </p>
 </div>

 

 <div class="refsect1 examples" id="refsect1-function.stream-wrapper-register-examples">
  <h3 class="title">范例</h3>
  <p class="para">
   <div class="example" id="example-4845">
    <p><strong>Example #1 如何注册一个 stream wrapper</strong></p>
    <div class="example-contents">
<div class="phpcode"><pre><span style="color: #000000">
<span style="color: #0000BB">&lt;?php<br />$existed&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #0000BB">in_array</span><span style="color: #007700">(</span><span style="color: #DD0000">"var"</span><span style="color: #007700">,&nbsp;</span><span style="color: #0000BB">stream_get_wrappers</span><span style="color: #007700">());<br />if&nbsp;(</span><span style="color: #0000BB">$existed</span><span style="color: #007700">)&nbsp;{<br />&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000BB">stream_wrapper_unregister</span><span style="color: #007700">(</span><span style="color: #DD0000">"var"</span><span style="color: #007700">);<br />}<br /></span><span style="color: #0000BB">stream_wrapper_register</span><span style="color: #007700">(</span><span style="color: #DD0000">"var"</span><span style="color: #007700">,&nbsp;</span><span style="color: #DD0000">"VariableStream"</span><span style="color: #007700">);<br /></span><span style="color: #0000BB">$myvar&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #DD0000">""</span><span style="color: #007700">;<br /><br /></span><span style="color: #0000BB">$fp&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #0000BB">fopen</span><span style="color: #007700">(</span><span style="color: #DD0000">"var://myvar"</span><span style="color: #007700">,&nbsp;</span><span style="color: #DD0000">"r+"</span><span style="color: #007700">);<br /><br /></span><span style="color: #0000BB">fwrite</span><span style="color: #007700">(</span><span style="color: #0000BB">$fp</span><span style="color: #007700">,&nbsp;</span><span style="color: #DD0000">"line1\n"</span><span style="color: #007700">);<br /></span><span style="color: #0000BB">fwrite</span><span style="color: #007700">(</span><span style="color: #0000BB">$fp</span><span style="color: #007700">,&nbsp;</span><span style="color: #DD0000">"line2\n"</span><span style="color: #007700">);<br /></span><span style="color: #0000BB">fwrite</span><span style="color: #007700">(</span><span style="color: #0000BB">$fp</span><span style="color: #007700">,&nbsp;</span><span style="color: #DD0000">"line3\n"</span><span style="color: #007700">);<br /><br /></span><span style="color: #0000BB">rewind</span><span style="color: #007700">(</span><span style="color: #0000BB">$fp</span><span style="color: #007700">);<br />while&nbsp;(!</span><span style="color: #0000BB">feof</span><span style="color: #007700">(</span><span style="color: #0000BB">$fp</span><span style="color: #007700">))&nbsp;{<br />&nbsp;&nbsp;&nbsp;&nbsp;echo&nbsp;</span><span style="color: #0000BB">fgets</span><span style="color: #007700">(</span><span style="color: #0000BB">$fp</span><span style="color: #007700">);<br />}<br /></span><span style="color: #0000BB">fclose</span><span style="color: #007700">(</span><span style="color: #0000BB">$fp</span><span style="color: #007700">);<br /></span><span style="color: #0000BB">var_dump</span><span style="color: #007700">(</span><span style="color: #0000BB">$myvar</span><span style="color: #007700">);<br /><br />if&nbsp;(</span><span style="color: #0000BB">$existed</span><span style="color: #007700">)&nbsp;{<br />&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000BB">stream_wrapper_restore</span><span style="color: #007700">(</span><span style="color: #DD0000">"var"</span><span style="color: #007700">);<br />}<br /><br /></span><span style="color: #0000BB">?&gt;</span>
</span>
</pre></div>
    </div>

    <div class="example-contents"><p>以上例程会输出：</p></div>
    <div class="example-contents screen">
<div class="cdata"><pre>
line1
line2
line3
string(18) &quot;line1
line2
line3
&quot;
</pre></div>
    </div>
   </div>
  </p>
 </div>


 <div class="refsect1 seealso" id="refsect1-function.stream-wrapper-register-seealso">
  <h3 class="title">参见</h3>
  <p class="para">
   <ul class="simplelist">
    <li class="member">The <a href="class.streamwrapper.html" class="xref">streamWrapper</a> prototype class</li>
    <li class="member"><a href="stream.streamwrapper.example_1.html" class="xref">Example class registered as stream wrapper</a></li>
    <li class="member"><span class="function"><a href="stream_wrapper_unregister.html" class="function" rel="rdfs-seeAlso">stream_wrapper_unregister()</a> - Unregister a URL wrapper</span></li>
    <li class="member"><span class="function"><a href="stream_wrapper_restore.html" class="function" rel="rdfs-seeAlso">stream_wrapper_restore()</a> - Restores a previously unregistered built-in wrapper</span></li>
    <li class="member"><span class="function"><a href="stream_get_wrappers.html" class="function" rel="rdfs-seeAlso">stream_get_wrappers()</a> - 获取已注册的流类型</span></li>
   </ul>
  </p>
 </div>



</div></div></div></body></html>